/**
 * 原数据 rootList
 * 期望结构见最下面
 *
 */
const rootList = [
  { id: 1, parent: null, text: '菜单1' },
  { id: 11, parent: 1, text: '菜单1-1' },
  { id: 12, parent: 1, text: '菜单1-2' },
  { id: 2, parent: null, text: '菜单2' },
  { id: 21, parent: 2, text: '菜单2-1' },
  { id: 3, parent: null, text: '菜单3' },
  { id: 31, parent: 3, text: '菜单3-1' },
  { id: 311, parent: 31, text: '菜单3-1-1' },
]
const getTreeList = (rootList, id, result) => {
  for (const item of rootList) {
    if (item.parent === id) result.push(item)
  }
  for (const item of result) {
    item.children = []
    // 递归深层遍历
    getTreeList(rootList, item.id, item.children)
    !item.children.length && delete item.children
  }
  return result
}

let res = getTreeList(rootList, null, [])

console.log(res)
console.log(JSON.stringify(res, null, 2))

/**
 * 思路分析：
 * 1.先拿一级菜单
 * 2.再根据一级菜单的与二级菜单之间的联系，来拿二级/三级/无限极... 菜单
 */

/**
期望的树形结构如下：
[
  {
    "id": 1,
    "parent": null,
    "text": "菜单1",
    "children": [
      {
        "id": 11,
        "parent": 1,
        "text": "菜单1-1"
      },
      {
        "id": 12,
        "parent": 1,
        "text": "菜单1-2"
      }
    ]
  },
  {
    "id": 2,
    "parent": null,
    "text": "菜单2",
    "children": [
      {
        "id": 21,
        "parent": 2,
        "text": "菜单2-1"
      }
    ]
  },
  {
    "id": 3,
    "parent": null,
    "text": "菜单3",
    "children": [
      {
        "id": 31,
        "parent": 3,
        "text": "菜单3-1",
        "children": [
          {
            "id": 311,
            "parent": 31,
            "text": "菜单3-1-1"
          }
        ]
      }
    ]
  }
]

 */
